//
//  StatisticsLayer.cpp
//  ATM Killer
//
//  Created by Cloner Team on 10/6/14.
//
//

#include "StatisticsLayer.h"
StatisticsLayer::StatisticsLayer() {
    this->gameModel = GameModel::getInstance();
    this->charactersDict = __Dictionary::create();
    CC_SAFE_RETAIN(this->charactersDict);
    this->avatarDict = __Dictionary::create();
    CC_SAFE_RETAIN(this->avatarDict);
    this->createLayers();
    this->createLayersContent();
    this->typeInfo = NUM_TYPE_STATS_GEN;
}

StatisticsLayer::~StatisticsLayer() {
    Utils::safeRelease(this->charactersDict);
}

void StatisticsLayer::onEnter() {
    Layer::onEnter();
    this->touchesRegister();
}

void StatisticsLayer::onExit() {
    Layer::onExit();
    this->removeAllChildrenWithCleanup(true);
}

void StatisticsLayer::touchesRegister() {
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan = CC_CALLBACK_2(StatisticsLayer::onTouchesBegan, this);
    listener->onTouchesMoved = CC_CALLBACK_2(StatisticsLayer::onTouchesMoved, this);
    listener->onTouchesEnded = CC_CALLBACK_2(StatisticsLayer::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void StatisticsLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
{
    if (this->gameModel->getGameState() == GS_STATISTICS)
        for (auto touch : touches) {
            Point point = this->convertTouchToNodeSpace(touch);
            Point statsPoint = point - Point(this->boardFace->getPositionX() - this->boardFace->getContentSize().width/2, this->boardFace->getPositionY() - this->boardFace->getContentSize().height/2);
            
            __Array* allKeys = this->avatarDict->allKeys();
            for (int i = 0; i < allKeys->count(); i++) {
                __Integer* key = (__Integer*) allKeys->getObjectAtIndex(i);
                Face* avatar = (Face*)this->avatarDict->objectForKey(key->getValue());
                RectBody* avatarRect = new RectBody(avatar->getContentSize().width, avatar->getContentSize().height);
                avatarRect->setPosition(Point(avatar->getPositionX(), avatar->getPositionY()));
                if (Utils::checkPointInRect(avatarRect, statsPoint)) {
                    this->typeInfo = key->getValue();
                }
            }
        }
}

void StatisticsLayer::onTouchesMoved(const std::vector<Touch*>& touches, Event* event){
}

void StatisticsLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event* event){
    
}

void StatisticsLayer::createLayers() {
    this->grayLayer = UGLayerColor::create();
    this->grayLayer->initWithColor(Color4B(0,0,0,100));
    this->addChild(this->grayLayer);
    
    this->boardLayer = UGLayer::create();
    this->addChild(this->boardLayer);
    
    this->generalLayer = UGLayer::create();
    this->characterLayer = UGLayer::create();
}

void StatisticsLayer::createLayersContent() {
    Size gameSize = this->gameModel->getDisplayResolutionSize();
    //boardLayer
    this->boardFace = new Face();
    this->boardFace->initWithSpriteFrameName((char*)ANI_BOARD_STATS_GEN);
    this->boardFace->setPosition(gameSize.width/2, gameSize.height/2);
    this->boardLayer->addChild(this->boardFace);
    
    //board title
    Label* titleBoardLabel = Utils::createLabel((char*)"General", (char*)FONT_TRANSFORMER, 45);
    titleBoardLabel->setColor(Color3B(255,255,255));
    titleBoardLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    titleBoardLabel->setAnchorPoint(Point(0,0.5f));
    titleBoardLabel->setPosition(50,560);
    this->boardFace->addChild(titleBoardLabel);
    titleBoardLabel->setTag(0);
    
    //version
    Label* versionLabel = Utils::createLabel((char*)"Version 1.2.1", (char*)FONT_MYRIADPRO_BOLD, 20);
    versionLabel->setColor(Color3B(128,215,255));
    versionLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    versionLabel->setAnchorPoint(Point(0,0.5f));
    versionLabel->setPosition(50,535);
    this->boardFace->addChild(versionLabel);
    
    //avatars
    for (int i = 0; i < 2; i++){
        Face* avatar = new Face();
        avatar->initWithSpriteFrameName(ANI_AVATAR_HOLDER_2);
        avatar->setPosition(610 + i * (avatar->getContentSize().width + 10), 70);
        
        if(i == 0) {
            Face* generalAvatar = new Face();
            generalAvatar->initWithSpriteFrameName(ANI_AVATAR_STAT_GENERAL);
            generalAvatar->setPosition(avatar->getContentSize().width / 2, avatar->getContentSize().height / 2);
            avatar->addChild(generalAvatar);
        } else if (i == 1) {
            Face* characterAvatar = new Face();
            characterAvatar->setPosition(avatar->getContentSize().width / 2, avatar->getContentSize().height / 2);
            characterAvatar->initWithSpriteFrameName(ANI_AVATAR_STAT_CHARACTERS);
            avatar->addChild(characterAvatar);
        }
        this->boardFace->addChild(avatar);
        avatarDict->setObject(avatar, i);
    }
    //close button
    UGMenu* closeMenu = UGMenu::create();
    closeMenu->setPosition(Point::ZERO);
    this->boardFace->addChild(closeMenu);
    
    MenuItemSprite* closeButton = Utils::createButton((char*)ANI_BUTTON_CLOSE_1, (char*)ANI_BUTTON_CLOSE_2);
    closeButton->setPosition(this->boardFace->getContentSize().width + closeButton->getContentSize().width/2 - 20, this->boardFace->getContentSize().height - closeButton->getContentSize().height/2);
    closeMenu->addChild(closeButton);
    closeButton->setCallback(CC_CALLBACK_1(StatisticsLayer::onCloseStatsBoard, this));
    
    //add board layer and character layer
    this->boardFace->addChild(this->generalLayer);
    this->generalLayer->setVisible(false);
    /*---------------GENERAL INFO----------------*/
    {
        //money in bank
        Label* moneyInBankLabel = Utils::createLabel((char*)"Money in bank:", (char*)FONT_TRANSFORMER, 20);
        moneyInBankLabel->setColor(Color3B(128,215,255));
        moneyInBankLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        moneyInBankLabel->setAnchorPoint(Point(0,0.5f));
        moneyInBankLabel->setPosition(55,485);
        this->generalLayer->addChild(moneyInBankLabel);
        
        Label* numMoneyInBank = Utils::createLabel((char*)"1234.567B", (char*)FONT_TRANSFORMER, 20);
        numMoneyInBank->setColor(Color3B(255,255,255));
        numMoneyInBank->setHorizontalAlignment(TextHAlignment::LEFT);
        numMoneyInBank->setAnchorPoint(Point(0,0.5f));
        numMoneyInBank->setPosition(moneyInBankLabel->getPositionX() + moneyInBankLabel->getContentSize().width, moneyInBankLabel->getPositionY());
        numMoneyInBank->setDimensions(300, numMoneyInBank->getContentSize().height);
        this->generalLayer->addChild(numMoneyInBank);
        numMoneyInBank->setTag(0);
        //total money
        Label* totalMoneyLabel = Utils::createLabel((char*)"Money all the time:", (char*)FONT_TRANSFORMER, 20);
        totalMoneyLabel->setColor(Color3B(128,215,255));
        totalMoneyLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        totalMoneyLabel->setAnchorPoint(Point(0,0.5f));
        totalMoneyLabel->setPosition(55,moneyInBankLabel->getPositionY() - 25);
        this->generalLayer->addChild(totalMoneyLabel);
        
        Label* numTotalMoney = Utils::createLabel((char*)"1234.567B", (char*)FONT_TRANSFORMER, 20);
        numTotalMoney->setColor(Color3B(255,255,255));
        numTotalMoney->setHorizontalAlignment(TextHAlignment::LEFT);
        numTotalMoney->setAnchorPoint(Point(0,0.5f));
        numTotalMoney->setPosition(totalMoneyLabel->getPositionX() + totalMoneyLabel->getContentSize().width, totalMoneyLabel->getPositionY());
        numTotalMoney->setDimensions(300, numTotalMoney->getContentSize().height);
        this->generalLayer->addChild(numTotalMoney);
        numTotalMoney->setTag(1);
        //current session
        Label* currentSessionLabel = Utils::createLabel((char*)"Current session:", (char*)FONT_TRANSFORMER, 20);
        currentSessionLabel->setColor(Color3B(128,215,255));
        currentSessionLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        currentSessionLabel->setAnchorPoint(Point(0,0.5f));
        currentSessionLabel->setPosition(55,moneyInBankLabel->getPositionY() - 50);
        this->generalLayer->addChild(currentSessionLabel);
        
        Label* numCurrentSession = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numCurrentSession->setColor(Color3B(255,255,255));
        numCurrentSession->setHorizontalAlignment(TextHAlignment::LEFT);
        numCurrentSession->setAnchorPoint(Point(0,0.5f));
        numCurrentSession->setPosition(currentSessionLabel->getPositionX() + currentSessionLabel->getContentSize().width, currentSessionLabel->getPositionY());
        numCurrentSession->setDimensions(300, numCurrentSession->getContentSize().height);
        this->generalLayer->addChild(numCurrentSession);
        numCurrentSession->setTag(2);
        //total time played
        Label* totalTimeLabel = Utils::createLabel((char*)"Total time played:", (char*)FONT_TRANSFORMER, 20);
        totalTimeLabel->setColor(Color3B(128,215,255));
        totalTimeLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        totalTimeLabel->setAnchorPoint(Point(0,0.5f));
        totalTimeLabel->setPosition(55,moneyInBankLabel->getPositionY() - 75);
        this->generalLayer->addChild(totalTimeLabel);
        
        Label* numTotalTime = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numTotalTime->setColor(Color3B(255,255,255));
        numTotalTime->setHorizontalAlignment(TextHAlignment::LEFT);
        numTotalTime->setAnchorPoint(Point(0,0.5f));
        numTotalTime->setPosition(totalTimeLabel->getPositionX() + totalTimeLabel->getContentSize().width, totalTimeLabel->getPositionY());
        numTotalTime->setDimensions(300, numTotalTime->getContentSize().height);
        this->generalLayer->addChild(numTotalTime);
        numTotalTime->setTag(3);
        //total Level
        Label* totalLevelLabel = Utils::createLabel((char*)"Total levels:", (char*)FONT_TRANSFORMER, 20);
        totalLevelLabel->setColor(Color3B(128,215,255));
        totalLevelLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        totalLevelLabel->setAnchorPoint(Point(0,0.5f));
        totalLevelLabel->setPosition(55,moneyInBankLabel->getPositionY() - 100);
        this->generalLayer->addChild(totalLevelLabel);
        
        Label* numTotalLevel = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numTotalLevel->setColor(Color3B(255,255,255));
        numTotalLevel->setHorizontalAlignment(TextHAlignment::LEFT);
        numTotalLevel->setAnchorPoint(Point(0,0.5f));
        numTotalLevel->setPosition(totalLevelLabel->getPositionX() + totalLevelLabel->getContentSize().width, totalLevelLabel->getPositionY());
        numTotalLevel->setDimensions(300, numTotalLevel->getContentSize().height);
        this->generalLayer->addChild(numTotalLevel);
        numTotalLevel->setTag(4);
        
        //total upgrade level
        Label* totalUpgradeLabel = Utils::createLabel((char*)"Total upgrade levels:", (char*) FONT_TRANSFORMER, 20);
        totalUpgradeLabel->setColor(Color3B(128,215,255));
        totalUpgradeLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        totalUpgradeLabel->setAnchorPoint(Point(0,0.5f));
        totalUpgradeLabel->setPosition(55,moneyInBankLabel->getPositionY() - 125);
        this->generalLayer->addChild(totalUpgradeLabel);
        
        Label* numTotalUpgrade = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numTotalUpgrade->setColor(Color3B(255,255,255));
        numTotalUpgrade->setHorizontalAlignment(TextHAlignment::LEFT);
        numTotalUpgrade->setAnchorPoint(Point(0,0.5f));
        numTotalUpgrade->setPosition(totalUpgradeLabel->getPositionX() + totalUpgradeLabel->getContentSize().width, totalUpgradeLabel->getPositionY());
        numTotalUpgrade->setDimensions(300, numTotalUpgrade->getContentSize().height);
        this->generalLayer->addChild(numTotalUpgrade);
        numTotalUpgrade->setTag(9);
        
        //money per sec
        Label* moneyPerSecond = Utils::createLabel((char*)"Money per second:", (char*)FONT_TRANSFORMER, 20);
        moneyPerSecond->setColor(Color3B(128,215,255));
        moneyPerSecond->setHorizontalAlignment(TextHAlignment::LEFT);
        moneyPerSecond->setAnchorPoint(Point(0,0.5f));
        moneyPerSecond->setPosition(55,moneyInBankLabel->getPositionY() - 150);
        this->generalLayer->addChild(moneyPerSecond);
        
        Label* numMoneyPerSec = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numMoneyPerSec->setColor(Color3B(255,255,255));
        numMoneyPerSec->setHorizontalAlignment(TextHAlignment::LEFT);
        numMoneyPerSec->setAnchorPoint(Point(0,0.5f));
        numMoneyPerSec->setPosition(moneyPerSecond->getPositionX() + moneyPerSecond->getContentSize().width, moneyPerSecond->getPositionY());
        numMoneyPerSec->setDimensions(300, numMoneyPerSec->getContentSize().height);
        this->generalLayer->addChild(numMoneyPerSec);
        numMoneyPerSec->setTag(5);
        //money per touch
        Label* moneyPerTouch = Utils::createLabel((char*)"Money per touch:", (char*)FONT_TRANSFORMER, 20);
        moneyPerTouch->setColor(Color3B(128,215,255));
        moneyPerTouch->setHorizontalAlignment(TextHAlignment::LEFT);
        moneyPerTouch->setAnchorPoint(Point(0,0.5f));
        moneyPerTouch->setPosition(55,moneyInBankLabel->getPositionY() - 175);
        this->generalLayer->addChild(moneyPerTouch);
        
        Label* numMoneyPerTouch = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numMoneyPerTouch->setColor(Color3B(255,255,255));
        numMoneyPerTouch->setHorizontalAlignment(TextHAlignment::LEFT);
        numMoneyPerTouch->setAnchorPoint(Point(0,0.5f));
        numMoneyPerTouch->setPosition(moneyPerTouch->getPositionX() + moneyPerTouch->getContentSize().width, moneyPerTouch->getPositionY());
        numMoneyPerTouch->setDimensions(300, numMoneyPerTouch->getContentSize().height);
        this->generalLayer->addChild(numMoneyPerTouch);
        numMoneyPerTouch->setTag(6);
        
        //total touch
        Label* totalTouch = Utils::createLabel((char*)"Total touch:", (char*)FONT_TRANSFORMER, 20);
        totalTouch->setColor(Color3B(128,215,255));
        totalTouch->setHorizontalAlignment(TextHAlignment::LEFT);
        totalTouch->setAnchorPoint(Point(0,0.5f));
        totalTouch->setPosition(55,moneyInBankLabel->getPositionY() - 200);
        this->generalLayer->addChild(totalTouch);
        
        Label* numTotalTouch = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numTotalTouch->setColor(Color3B(255,255,255));
        numTotalTouch->setHorizontalAlignment(TextHAlignment::LEFT);
        numTotalTouch->setAnchorPoint(Point(0,0.5f));
        numTotalTouch->setPosition(totalTouch->getPositionX() + totalTouch->getContentSize().width, totalTouch->getPositionY());
        numTotalTouch->setDimensions(300, numTotalTouch->getContentSize().height);
        this->generalLayer->addChild(numTotalTouch);
        numTotalTouch->setTag(7);
        //money by touch
        Label* moneyByTouch = Utils::createLabel((char*)"Money by touch:", (char*)FONT_TRANSFORMER, 20);
        moneyByTouch->setColor(Color3B(128,215,255));
        moneyByTouch->setHorizontalAlignment(TextHAlignment::LEFT);
        moneyByTouch->setAnchorPoint(Point(0,0.5f));
        moneyByTouch->setPosition(55,moneyInBankLabel->getPositionY() - 225);
        this->generalLayer->addChild(moneyByTouch);
        
        Label* numMoneyByTouch = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numMoneyByTouch->setColor(Color3B(255,255,255));
        numMoneyByTouch->setHorizontalAlignment(TextHAlignment::LEFT);
        numMoneyByTouch->setAnchorPoint(Point(0,0.5f));
        numMoneyByTouch->setPosition(moneyByTouch->getPositionX() + moneyByTouch->getContentSize().width, moneyByTouch->getPositionY());
        numMoneyByTouch->setDimensions(300, numMoneyByTouch->getContentSize().height);
        this->generalLayer->addChild(numMoneyByTouch);
        numMoneyByTouch->setTag(8);
        
        //total unlocked Achivement
        Label* unlockAchievement = Utils::createLabel((char*)"Number of unlocked achievements:", (char*)FONT_TRANSFORMER, 20);
        unlockAchievement->setColor(Color3B(128,215,255));
        unlockAchievement->setHorizontalAlignment(TextHAlignment::LEFT);
        unlockAchievement->setAnchorPoint(Point(0,0.5f));
        unlockAchievement->setPosition(55,moneyInBankLabel->getPositionY() - 250);
        this->generalLayer->addChild(unlockAchievement);
        
        Label* numUnlockAchievement = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numUnlockAchievement->setColor(Color3B(255,255,255));
        numUnlockAchievement->setHorizontalAlignment(TextHAlignment::LEFT);
        numUnlockAchievement->setAnchorPoint(Point(0,0.5f));
        numUnlockAchievement->setPosition(unlockAchievement->getPositionX() + unlockAchievement->getContentSize().width, unlockAchievement->getPositionY());
        numUnlockAchievement->setDimensions(300, numUnlockAchievement->getContentSize().height);
        this->generalLayer->addChild(numUnlockAchievement);
        numUnlockAchievement->setTag(10);
        
        //bonus Level
        Label* atmBonus = Utils::createLabel((char*)"ATM upgrade level:", (char*)FONT_TRANSFORMER, 20);
        atmBonus->setColor(Color3B(128,215,255));
        atmBonus->setHorizontalAlignment(TextHAlignment::LEFT);
        atmBonus->setAnchorPoint(Point(0,0.5f));
        atmBonus->setPosition(55,moneyInBankLabel->getPositionY() - 275);
        this->generalLayer->addChild(atmBonus);
        
        Label* numAtmBonus = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numAtmBonus->setColor(Color3B(255,255,255));
        numAtmBonus->setHorizontalAlignment(TextHAlignment::LEFT);
        numAtmBonus->setAnchorPoint(Point(0,0.5f));
        numAtmBonus->setPosition(atmBonus->getPositionX() + atmBonus->getContentSize().width, atmBonus->getPositionY());
        numAtmBonus->setDimensions(300, numAtmBonus->getContentSize().height);
        this->generalLayer->addChild(numAtmBonus);
        numAtmBonus->setTag(11);
        
        //total bonus atm
        Label* atmTotalBonusPercents = Utils::createLabel((char*)"Total bonus percents of the ATM:", (char*)FONT_TRANSFORMER, 20);
        atmTotalBonusPercents->setColor(Color3B(128,215,255));
        atmTotalBonusPercents->setHorizontalAlignment(TextHAlignment::LEFT);
        atmTotalBonusPercents->setAnchorPoint(Point(0,0.5f));
        atmTotalBonusPercents->setPosition(55,moneyInBankLabel->getPositionY() - 300);
        this->generalLayer->addChild(atmTotalBonusPercents);
        
        Label* numTotalBonusPercents = Utils::createLabel((char*)"999 hours ago", (char*)FONT_TRANSFORMER, 20);
        numTotalBonusPercents->setColor(Color3B(255,255,255));
        numTotalBonusPercents->setHorizontalAlignment(TextHAlignment::LEFT);
        numTotalBonusPercents->setAnchorPoint(Point(0,0.5f));
        numTotalBonusPercents->setPosition(atmTotalBonusPercents->getPositionX() + atmTotalBonusPercents->getContentSize().width, atmTotalBonusPercents->getPositionY());
        numTotalBonusPercents->setDimensions(300, numTotalBonusPercents->getContentSize().height);
        this->generalLayer->addChild(numTotalBonusPercents);
        numTotalBonusPercents->setTag(12);
        
        UGMenu* facebookMenu = UGMenu::create();
        facebookMenu->setPosition(Point::ZERO);
        this->generalLayer->addChild(facebookMenu);
        
        this->facebookButton = Utils::createButton((char*)ANI_BUTTON_STATS_FACEBOOK, (char*)ANI_BUTTON_STATS_FACEBOOK);
        this->facebookButton->setPosition(100,100);
        this->facebookButton->setCallback(CC_CALLBACK_1(StatisticsLayer::onButtonClick, this));
        facebookMenu->addChild(this->facebookButton);
    }
    /*---------------CHARACTER INFO----------------*/
    {
        __Array* allKeys = this->gameModel->getCharacters()->allKeys();
        for (int i = 0; i < allKeys->count(); i++) {
            __Integer* key = (__Integer*)allKeys->getObjectAtIndex(i);
            Character* currentCharacter = (Character*)this->gameModel->getCharacters()->objectForKey(key->getValue());
            EntityCharacter* entityCharacter = (EntityCharacter*)this->gameModel->getEttRepository()->getCharacterList()->objectForKey(key->getValue());
            UGLayer* eachCharacterLayer = UGLayer::create();
            this->characterLayer->addChild(eachCharacterLayer);
            eachCharacterLayer->setContentSize(Size(435,120));
            eachCharacterLayer->setPosition(Point(50, 1120 - 140*key->getValue()));
            //avatar Face
            Face* avatar = new Face();
            avatar->initWithSpriteFrameName(entityCharacter->getAvatarImage());
            avatar->setPosition(avatar->getContentSize().width/2, 90);
            eachCharacterLayer->addChild(avatar);
            //character name label
            char characterName[40];
            sprintf(characterName, "%s", currentCharacter->getName().c_str());
            Label* name = Utils::createLabel(characterName, (char*)FONT_TRANSFORMER, 28);
            name->setAnchorPoint(Point(0,0.5));
            name->setHorizontalAlignment(TextHAlignment::LEFT);
            name->setPosition(65, 105);
            eachCharacterLayer->addChild(name);
            //character level
            Label* levelLabel = Utils::createLabel((char*)"Level: ", (char*) FONT_TRANSFORMER, 20);
            levelLabel->setColor(Color3B(128, 215, 255));
            levelLabel->setAnchorPoint(Point(0,0.5));
            levelLabel->setHorizontalAlignment(TextHAlignment::LEFT);
            levelLabel->setPosition(65, 75);
            eachCharacterLayer->addChild(levelLabel);
            
            Label* numLevelLabel = Utils::createLabel((char*)"250", (char*) FONT_TRANSFORMER, 20);
            numLevelLabel->setAnchorPoint(Point(0, 0.5));
            numLevelLabel->setHorizontalAlignment(TextHAlignment::LEFT);
            numLevelLabel->setPosition(levelLabel->getPositionX() + levelLabel->getContentSize().width - 20, levelLabel->getPositionY());
            numLevelLabel->setDimensions(400, numLevelLabel->getContentSize().height);
            eachCharacterLayer->addChild(numLevelLabel);
            numLevelLabel->setTag(0);
            //total money get
            Label* totalMoneyLabel = Utils::createLabel((char*)"Total money get: ", (char*) FONT_TRANSFORMER, 20);
            totalMoneyLabel->setColor(Color3B(128, 215, 255));
            totalMoneyLabel->setAnchorPoint(Point(0,0.5));
            totalMoneyLabel->setHorizontalAlignment(TextHAlignment::LEFT);
            totalMoneyLabel->setPosition(65, 55);
            eachCharacterLayer->addChild(totalMoneyLabel);
            
            Label* numTotalMoney = Utils::createLabel((char*)"250", (char*) FONT_TRANSFORMER, 20);
            numTotalMoney->setAnchorPoint(Point(0, 0.5));
            numTotalMoney->setHorizontalAlignment(TextHAlignment::LEFT);
            numTotalMoney->setPosition(totalMoneyLabel->getPositionX() + totalMoneyLabel->getContentSize().width - 20, totalMoneyLabel->getPositionY());
            numTotalMoney->setDimensions(400, numTotalMoney->getContentSize().height);
            eachCharacterLayer->addChild(numTotalMoney);
            numTotalMoney->setTag(1);
            //money per seconds
            Label* moneyPerSecond = Utils::createLabel((char*)"Money per second: ", (char*)FONT_TRANSFORMER, 20);
            moneyPerSecond->setColor(Color3B(128, 215, 255));
            moneyPerSecond->setAnchorPoint(Point(0, 0.5));
            moneyPerSecond->setHorizontalAlignment(TextHAlignment::LEFT);
            moneyPerSecond->setPosition(65, 35);
            eachCharacterLayer->addChild(moneyPerSecond);
            
            Label* numMoneyPerSec = Utils::createLabel((char*)"250", (char*) FONT_TRANSFORMER, 20);
            numMoneyPerSec->setAnchorPoint(Point(0, 0.5));
            numMoneyPerSec->setHorizontalAlignment(TextHAlignment::LEFT);
            numMoneyPerSec->setPosition(moneyPerSecond->getPositionX() + moneyPerSecond->getContentSize().width - 20, moneyPerSecond->getPositionY());
            numMoneyPerSec->setDimensions(47700, numMoneyPerSec->getContentSize().height);
            eachCharacterLayer->addChild(numMoneyPerSec);
            numMoneyPerSec->setTag(2);
            //base mps
            Label* baseMps = Utils::createLabel((char*)"Base Mps: ", (char*)FONT_TRANSFORMER, 20);
            baseMps->setColor(Color3B(128, 215, 255));
            baseMps->setAnchorPoint(Point(0, 0.5));
            baseMps->setHorizontalAlignment(TextHAlignment::LEFT);
            baseMps->setPosition(65, 15);
            eachCharacterLayer->addChild(baseMps);
            
            Label* numBaseMps = Utils::createLabel((char*)"250", (char*) FONT_TRANSFORMER, 20);
            numBaseMps->setAnchorPoint(Point(0, 0.5));
            numBaseMps->setHorizontalAlignment(TextHAlignment::LEFT);
            numBaseMps->setPosition(baseMps->getPositionX() + baseMps->getContentSize().width - 20, baseMps->getPositionY());
            numBaseMps->setDimensions(47700, numBaseMps->getContentSize().height);
            eachCharacterLayer->addChild(numBaseMps);
            numBaseMps->setTag(3);

            
            //time play
            //            Label* timePlay = Utils::createLabel((char*)"Time play: ", (char*)FONT_TRANSFORMER, 20);
            //            timePlay->setColor(Color3B(128, 215, 255));
            //            timePlay->setAnchorPoint(Point(0,0.5));
            //            timePlay->setHorizontalAlignment(TextHAlignment::LEFT);
            //            timePlay->setPosition(65,15);
            //            eachCharacterLayer->addChild(timePlay);
            //
            //            Label* numTimePlay = Utils::createLabel((char*)"250", (char*) FONT_TRANSFORMER, 20);
            //            numTimePlay->setAnchorPoint(Point(0, 0.5));
            //            numTimePlay->setHorizontalAlignment(TextHAlignment::LEFT);
            //            numTimePlay->setPosition(timePlay->getPositionX() + timePlay->getContentSize().width, timePlay->getPositionY());
            //            eachCharacterLayer->addChild(numTimePlay);
            //            numTimePlay->setTag(3);
            
            this->charactersDict->setObject(eachCharacterLayer, currentCharacter->getCharacterId());
            
        }
        this->characterLayer->setContentSize(Size(500,1250));
        this->characterContentView = ScrollView::create(Size(500, 450));
        this->characterContentView->setPosition(5,50);
        this->characterContentView->setContainer(this->characterLayer);
        this->characterContentView->setDirection(ScrollView::Direction::VERTICAL);
        this->characterContentView->setContentOffset(Point(0,-800));
        this->boardFace->addChild(this->characterContentView);
        
        //size
        //GENERAL : 45
        //Version: 30 Color(128,215,255)
        //Money in bank : 20
    }
}

void StatisticsLayer::onCloseStatsBoard(Ref* sender){
    this->notify(REQ_PAUSE, EVT_CLICK_BUTTON, NULL, 0, NULL);
}

void StatisticsLayer::update(float dt) {
    if (gameModel->getGameState() == GS_STATISTICS){
        this->grayLayer->setVisible(true);
        this->boardLayer->setVisible(true);
    } else {
        this->grayLayer->setVisible(false);
        this->boardLayer->setVisible(false);
    }
    // update avatar
    {
        __Array* allKeys = this->avatarDict->allKeys();
        for (int i = 0; i < allKeys->count(); i++) {
            __Integer* key = (__Integer*) allKeys->getObjectAtIndex(i);
            Face* avatar = (Face*)this->avatarDict->objectForKey(key->getValue());
            if (key->getValue() == this->typeInfo){
                avatar->setSpriteFrame(ANI_AVATAR_HOLDER_1);
            } else {
                avatar->setSpriteFrame(ANI_AVATAR_HOLDER_2);
            }
            
        }
    }
    // update type of info
    {
        Label* title = (Label*) this->boardFace->getChildByTag(0);
        if(this->typeInfo == NUM_TYPE_STATS_GEN) {
            this->boardFace->setSpriteFrame(ANI_BOARD_STATS_GEN);
            title->setString("GENERAL");
            this->characterContentView->setVisible(false);
            this->generalLayer->setVisible(true);
        } else if (this->typeInfo == NUM_TYPE_STATS_CHAR) {
            this->boardFace->setSpriteFrame(ANI_BOARD_STATS_CHAR);
            title->setString("CHARACTERS");
            this->characterContentView->setVisible(true);
            this->generalLayer->setVisible(false);
        }
    }
    /*---------------GENERAL INFO----------------*/
    {
        //money in bank
        char moneyInBank[100];
        char* moneyInBankText = this->gameModel->getScore()->display();
        sprintf(moneyInBank, "$ %s", moneyInBankText);
        delete [] moneyInBankText;
        Label* numMoneyInBank = (Label*) this->generalLayer->getChildByTag(0);
        numMoneyInBank->setString(moneyInBank);
        
        //total money
        char totalMoney[100];
        char* totalMoneyText = this->gameModel->getCounter()->getTotalMoney()->display();
        sprintf(totalMoney, "$ %s", totalMoneyText);
        delete[] totalMoneyText;
        Label* numTotalMoney = (Label*) this->generalLayer->getChildByTag(1);
        numTotalMoney->setString(totalMoney);
        
        //current session
        char currentSession[100];
        double currentSessionText = this->gameModel->getCounter()->getTimeSession();
        sprintf(currentSession, "%s", Utils::convertTimeString(currentSessionText).c_str());
        Label* numCurrentSession = (Label*) this->generalLayer->getChildByTag(2);
        numCurrentSession->setString(currentSession);
        
        //total time
        char totalTime[100];
        int totalTimeText = (int)this->gameModel->getCounter()->getTimePlay();
        sprintf(totalTime, "%s", Utils::convertTimeString(totalTimeText).c_str());
        Label* numTotalTime = (Label*)this->generalLayer->getChildByTag(3);
        numTotalTime->setString(totalTime);
        
        //total level
        char totalLevel[100];
        sprintf(totalLevel, "%d levels", this->gameModel->getTotalCharactersLevel());
        Label* numTotalLevel = (Label*)this->generalLayer->getChildByTag(4);
        numTotalLevel->setString(totalLevel);
        
        //total upgrade level
        char totalUpgradeLevel[100];
        sprintf(totalUpgradeLevel, "%d levels", this->gameModel->getTotalCharactersUpgrade());
        Label* numTotalUpgrade = (Label*)this->generalLayer->getChildByTag(9);
        numTotalUpgrade->setString(totalUpgradeLevel);
        
        //moneyPerSec
        char moneyPerSec[100];
        char* moneyPerSecText = this->gameModel->getCounter()->getTotalSpeed()->display();
        sprintf(moneyPerSec, "$ %s", moneyPerSecText);
        delete[] moneyPerSecText;
        Label* numMoneyPerSec = (Label*) this->generalLayer->getChildByTag(5);
        numMoneyPerSec->setString(moneyPerSec);
        
        //moneyPerTouch
        char moneyPerTouch[100];
        char* moneyPerTouchText = this->gameModel->getCounter()->getMoneyPerTouch()->display();
        sprintf(moneyPerTouch, "$ %s", moneyPerTouchText);
        delete[] moneyPerTouchText;
        Label* numMoneyPerTouch = (Label*) this->generalLayer->getChildByTag(6);
        numMoneyPerTouch->setString(moneyPerTouch);
        
        //totalTouch
        char totalTouch[100];
        char* totalTouchText = this->gameModel->getCounter()->getTouchNumbers()->display();
        sprintf(totalTouch, "%s times", totalTouchText);
        delete[] totalTouchText;
        Label* numTotalTouch = (Label*) this->generalLayer->getChildByTag(7);
        numTotalTouch->setString(totalTouch);
        
        //money by touch
        char moneyByTouch[100];
        char* moneyByTouchText = this->gameModel->getCounter()->getTouchMoney()->display();
        sprintf(moneyByTouch, "$ %s", moneyByTouchText);
        delete[] moneyByTouchText;
        Label* numMoneyByTouch = (Label*) this->generalLayer->getChildByTag(8);
        numMoneyByTouch->setString(moneyByTouch);
        
        //unlock achievements
        char unlockAchievement[5];
        sprintf(unlockAchievement, "%d", this->gameModel->numOfUnlockedAchi);
        Label* numUnlockAchievement = (Label*) this->generalLayer->getChildByTag(10);
        numUnlockAchievement->setString(unlockAchievement);
        
        //atm bonus
        char atmBonus[5];
        sprintf(atmBonus, "%d levels", this->gameModel->getAtm()->getBonusLevel());
        Label* numAtmBonus = (Label*) this->generalLayer->getChildByTag(11);
        numAtmBonus->setString(atmBonus);
        
        //total atm bonus percents
        char atmBonusPercents[20];
        sprintf(atmBonusPercents, "%.2f %%", this->gameModel->getCounter()->getTotalBonusPercentATM());
        Label* numTotalBonusPercents = (Label*) this->generalLayer->getChildByTag(12);
        numTotalBonusPercents->setString(atmBonusPercents);
    }
    
    /*---------------CHARACTER INFO----------------*/
    {
        __Array* allKeys = this->charactersDict->allKeys();
        for(int i = 0; i < allKeys->count(); i++) {
            __Integer* key = (__Integer*) allKeys->getObjectAtIndex(i);
            UGLayer* currentLayer = (UGLayer*) this->charactersDict->objectForKey(key->getValue());
            Character* currentCharacter = (Character*)this->gameModel->getCharacters()->objectForKey(key->getValue());
            
            //numLevelLabel
            char characterLevel[10];
            sprintf(characterLevel, "%d", currentCharacter->getLevel());
            Label* numLevelLabel = (Label*)currentLayer->getChildByTag(0);
            numLevelLabel->setString(characterLevel);
            
            //numTotalMoneyGet
            char totalMoneyGet[100];
            char* totalMoneyGetText = currentCharacter->getCharacterMoney()->display();
            sprintf(totalMoneyGet, "$ %s", totalMoneyGetText);
            delete[] totalMoneyGetText;
            Label* numTotalMoneyGet = (Label*)currentLayer->getChildByTag(1);
            numTotalMoneyGet->setString(totalMoneyGet);
            
            //moneyPerSeconds
            char moneyPerSeconds[100];
            char* moneyPerSecondsText = currentCharacter->getMoneyPerSec()->display();
            sprintf(moneyPerSeconds, "$ %s", moneyPerSecondsText);
            delete[] moneyPerSecondsText;
            Label* numMoneyPerSec = (Label*) currentLayer->getChildByTag(2);
            numMoneyPerSec->setString(moneyPerSeconds);
            
            //moneyPerAttack
            char numBaseMps[100];
            char* numBaseMpsText = currentCharacter->getDps()->display();
            sprintf(numBaseMps, "$ %s", numBaseMpsText);
            delete[] numBaseMpsText;
            Label* numBaseMpsLabel = (Label*) currentLayer->getChildByTag(3);
            numBaseMpsLabel->setString(numBaseMps);
        }
    }
    
}

void StatisticsLayer::onButtonClick(Ref* sender) {
    if(sender == this->facebookButton) {
        char highScoreText[100];
        char* moneyText = this->gameModel->getCounter()->getTotalMoney()->display();
        sprintf(highScoreText,"I've get total $ %s. Am I the richest all over the world?Oh oh, beat me please!!!!!!", moneyText);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        AppUtils::shareFacebook("Clicker Heroes", highScoreText ,"https://farm8.staticflickr.com/7493/15102766204_3de1b1e361_s.jpg", "https://itunes.apple.com/us/app/clicker-heroes/id926654548?ls=1&mt=8", "Sorry! Cannot connect to your facebook");
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        char highScore[30];
        sprintf(highScore,":callFacebook:%s",moneyText);
        Application* application = Application::getInstance();
        application->getURL(highScore);
#endif
        delete[] moneyText;
    }
}

